package com.coolwen.spring.jdbc.config;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @author CoolWen
 * @version 2020-04-09 10:07
 */
@Configuration
@ComponentScan("com.coolwen.spring.jdbc")
@PropertySource(value = "classpath:jdbc.properties")
@EnableTransactionManagement
public class Config {

    @Value("${jdbc.driverClassName}")
    String driverClassName;

    @Value("${jdbc.url}")
    String url;

    @Value("${jdbc.username}")
    String username;


    @Value("${jdbc.password}")
    String password;

    @Bean
    public BasicDataSource dataSource() {
        BasicDataSource basicDataSource = new BasicDataSource();
        System.out.println(driverClassName);
        basicDataSource.setDriverClassName(driverClassName);
        basicDataSource.setUrl(url);
        basicDataSource.setUsername(username);
        basicDataSource.setPassword(password);
        return basicDataSource;
    }

//    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
//    <property name="driverClassName" value="${jdbc.driverClassName}" />
//    <property name="url" value="${jdbc.url}" />
//    <property name="username" value="${jdbc.username}" />
//    <property name="password" value="${jdbc.password}" />
//</bean>

    @Bean
    public DataSourceTransactionManager txmManager() {
        DataSourceTransactionManager txmTransactionManager = new DataSourceTransactionManager();
        txmTransactionManager.setDataSource(dataSource());
        return txmTransactionManager;
    }


//    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
//    <property name="dataSource" ref="dataSource"/>
//</bean>
}
